import {
  world,
  EquipmentSlot,
  Player,
  ItemStack,
  GameMode,
} from "@minecraft/server";
import { utils } from "../..";

/**
 * Define a tool tag.
 */
export class WeaponTag {
  constructor(
    /**
     * The weapon tag.
     */
    public tag: string,
    /**
     * Type of the weapon.
     */
    public type?: WeaponType
  ) {
    this.listeningDurability();
  }
  /**
   * Automatically consume durability for the tools.
   */
  listeningDurability() {
    world.afterEvents.playerBreakBlock.subscribe((event) => {
      const [PLAYER, ITEM] = [
        event.player,
        utils.getEquipmentItem(event.player),
      ];
      if (
        PLAYER.getGameMode() === GameMode.survival ||
        PLAYER.getGameMode() === GameMode.adventure
      ) {
        if (ITEM?.hasTag(this.tag)) {
          PLAYER?.getComponent("minecraft:equippable")?.setEquipment(
            EquipmentSlot.Mainhand,
            utils.consumeDurability(ITEM, 2, PLAYER)
          );
        }
      }
    });
    world.afterEvents.entityHitEntity.subscribe((event) => {
      const [ENTITY, ITEM] = [
        event.damagingEntity,
        utils.getEquipmentItem(event.damagingEntity),
      ];
      if(ENTITY instanceof Player){
        if (
          ENTITY.getGameMode() === GameMode.survival ||
          ENTITY.getGameMode() === GameMode.adventure
        ) {
          if (ITEM?.hasTag(this.tag)) {
            ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
              EquipmentSlot.Mainhand,
              utils.consumeDurability(ITEM, 1, ENTITY)
            );
          }
        }
      }else if (ITEM?.hasTag(this.tag)) {
        ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
          EquipmentSlot.Mainhand,
          utils.consumeDurability(
            ITEM,
            1,
            ENTITY instanceof Player ? ENTITY : undefined
          )
        );
      }
    });
  }
}

/**
 * Define a tool.
 */
export class WeaponItem {
  constructor(
    /**
     * Item of the weapon.
     */
    public item: ItemStack,
    /**
     * Type of the weapon.
     */
    public type?: WeaponType
  ) {
    this.listeningDurability();
  }
  /**
   * Automatically consume durability for the tools.
   */
  listeningDurability() {
    world.afterEvents.playerBreakBlock.subscribe((event) => {
      const [PLAYER, ITEM] = [
        event.player,
        utils.getEquipmentItem(event.player),
      ];
      if (
        PLAYER.getGameMode() === GameMode.survival ||
        PLAYER.getGameMode() === GameMode.adventure
      ) {
        if (ITEM?.typeId===this.item.typeId) {
          PLAYER?.getComponent("minecraft:equippable")?.setEquipment(
            EquipmentSlot.Mainhand,
            utils.consumeDurability(ITEM, 2, PLAYER)
          );
        }
      }
    });
    world.afterEvents.entityHitEntity.subscribe((event) => {
      const [ENTITY, ITEM] = [
        event.damagingEntity,
        utils.getEquipmentItem(event.damagingEntity),
      ];
      if(ENTITY instanceof Player){
        if (
          ENTITY.getGameMode() === GameMode.survival ||
          ENTITY.getGameMode() === GameMode.adventure
        ) {
          if (ITEM?.typeId===this.item.typeId) {
            ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
              EquipmentSlot.Mainhand,
              utils.consumeDurability(ITEM, 1, ENTITY)
            );
          }
        }
      }else if (ITEM?.typeId===this.item.typeId) {
        ENTITY?.getComponent("minecraft:equippable")?.setEquipment(
          EquipmentSlot.Mainhand,
          utils.consumeDurability(
            ITEM,
            1,
            ENTITY instanceof Player ? ENTITY : undefined
          )
        );
      }
    });
  }
}

/**
 * Type of the weapon.
 */
export enum WeaponType {
  SWORD,
  CUSTOM,
}
